package com.qf.test;

import com.qf.pojo.User;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
import redis.clients.jedis.Jedis;

import java.util.Date;

/**
 * @author 千锋健哥
 */
public class Demo1 {

    /**
     * 以byte数组形式保存实体
     */
    @Test
    public void setByteArray() {
        Jedis jedis = new Jedis("192.168.200.129", 6379);

        String key = "user";
        User value = new User(1, "青龙", new Date());

        byte[] keyByte = SerializationUtils.serialize(key);
        byte[] valueByte = SerializationUtils.serialize(value);

        jedis.set(keyByte, valueByte);
    }

    /**
     * 获取序列化后byte形式数组的数据
     */
    @Test
    public void getByteArray() {
        Jedis jedis = new Jedis("192.168.200.129", 6379);

        String key = "user";
        byte[] keyByte = SerializationUtils.serialize(key);

        //获取数据
        byte[] valueByte = jedis.get(keyByte);
        User user = (User)SerializationUtils.deserialize(valueByte);
        System.out.println("=======" + user);
    }
}
